<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js"></script>
<title></title>
<script type="text/javascript">
    var userId = '${__SATURN_AUTORITY_USER__.id}';
	Ext.onReady(function(){
		Ext.QuickTips.init();
		var resultWidth = Ext.get("task-grid").getWidth();
		var formPanel = new Ext.FormPanel({
            frame: true,
	        title:'委托的任务',
	        labelWidth:100,
	        renderTo:'form-ct',
	        width:resultWidth,
			items: [{
	              xtype:'fieldset',
           		  title: '选项',
          		  autoHeight: true,
          		  width:resultWidth,
            	  layout: 'form',
		     	  items: [{　
	                xtype: 'checkboxgroup',
	                fieldLabel: '任务来源',  
	                items: [
	                   {boxLabel: '代办任务', id: 'cbDelegateTask', checked: true},
	                   {boxLabel: '协办任务', id: 'cbHelpTask',checked: true}
	                ]
            	}]
            }],
             buttons: [{
	            text: '查询',
	            handler:function(){
	            	if(isValid()){
						var task = getBizState();          		
	            		store.load({params:{	
	            						start:0,
	            						limit:20,
	            						state:10,			
										userid:userId,
										bizstate:task }}); 
		
		       			grid.getView().refresh();
	            	}else{
	            		Ext.MessageBox.alert("请选择任务来源");
	            	}
	            }
       		 }]
		});
		
		function getBizState(){
			var cbDelegateTask = Ext.get("cbDelegateTask").dom.checked;
			var cbHelpTask = Ext.get("cbHelpTask").dom.checked;
			var task = "";
       		if(cbDelegateTask){
       			task = task + "2,";
       		}
       		if(cbHelpTask){
       			task = task + "3,6,";
       		}
       		return task.substring(0,task.length-1);
		}
		
		function isValid(){
			var flag = true;
			var cbHelpTask = Ext.get("cbHelpTask").dom.checked;
			var cbDelegateTask = Ext.get("cbDelegateTask").dom.checked;
			if(cbHelpTask==false&&cbDelegateTask==false){
			   flag = false;
			}
			return flag;
		}
		
		var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/QueryDelegateTaskByUser.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
	               {name: 'workitem_name', mapping: 'workitem_name'},
	               {name: 'process_instance_id', mapping: 'process_instance_id'},
		    	   {name: 'workitem_id', mapping: 'workitem_id'},
	               {name: 'process_define_name', mapping: 'process_define_name'},
	               {name: 'process_instance_name', mapping: 'process_instance_name'},
	               {name: 'workitem_state', mapping: 'workitem_state'},
	               {name: 'workitem_start_time', mapping: 'workitem_start_time'},
	               {name: 'workitem_is_timeout', mapping: 'workitem_is_timeout'},
	               {name: 'limit_number', mapping: 'limit_number'}
	           ]
		});
	  
	    var grid = new Ext.grid.GridPanel({
       	    store: store,
	        columns: [
	       	    {header: "操作", width: resultWidth*0.1, dataIndex: 'workitem_id', sortable: false,renderer:showOp},
	            {header: "工作项名称", width: resultWidth*0.15,dataIndex: 'workitem_name', sortable: true,renderer:showOpMesg},
	            {header: "业务流程名称", width: resultWidth*0.15,dataIndex: 'process_define_name', sortable: true},
	            {header: "流程实例名称", width: resultWidth*0.1,dataIndex: 'process_instance_name', sortable: true},
	            {header: "当前状态", width: resultWidth*0.1,dataIndex: 'workitem_state', sortable: true,renderer:showState},
	            {header: "启动时间", width: resultWidth*0.15,dataIndex: 'workitem_start_time', sortable: true},
	            {header: "是否超时", width: resultWidth*0.1, dataIndex: 'workitem_is_timeout', sortable: true,renderer:showIsTimeout},
	            {header: "时间限制", width: resultWidth*0.1,dataIndex: 'limit_number', sortable: true}
	        ],
	        renderTo:'task-grid',
	        width:resultWidth,
	        height:400,
	        loadMask: {msg:'正在加载数据，请稍侯……'}, 
	        frame:true,
	        bodyStyle:'padding:5px 5px 0;width:100%',
	        bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
		    }) ,
		     tbar:new Array(
			   new Ext.Toolbar.Button({
						   text:'只显示超时任务',
						   xtype:"button" ,
						   tooltip:"只显示超时任务" ,
						   handler: queryTaskOfisTimeout,
						   scope :this})
			   
		   ) 
    });
  
    var str = "2,3,6";
   	store.load({params: {start:0,limit:20,state:10,userid:userId,bizstate:str}});
   	
   	store.on('beforeload',function(){
   		if(isValid()){
	   		 var task = getBizState();
		     Ext.apply(this.baseParams,{state:10,userid:userId,bizstate:task});		
        }else{
        	Ext.MessageBox.alert("请选择任务来源");
        	return false;
        }
    });
    
    function queryTaskOfisTimeout(){
		store.load({params: {workitem_is_timeout:'Y'}});
	}
	
   	function showOp(value,cellmeta,record){	
			return "<a href='javascript:doView("+record.data["process_instance_id"]+")'>"+"查看"+"  </a><a href='javascript:withdrow("+value+")'>"+"收回"+"</a>";	
	}	
	
	function showOpMesg(value,cellmeta,record){	
			return "<a href='javascript:showWorkitemInfo("+record.data["workitem_id"]+")'>"+value+"</a>";	
	}	
});

	function showWorkitemInfo(workitem_id){	
		 window.location="${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client/myTaskModule/page/workitemOpMesg.jsp?workitem_id="+workitem_id;
	}
	function doView(process_instance_id){
		 var form = document.forms[0];
		 form.action="${pageContext.request.contextPath}/edu.ccut.saturn.workflow::/publicModule/action/DelineateWorkFlow.action?process_instance_id="+process_instance_id;
		 form.submit();
	}
	function showState(value){
		if(value=='10')
			return "运行";
		if(value=='12')
			return "完成";
	}
	
	function showIsTimeout(value){
		if(value=="Y"){
			return "<font color='red'>"+"是"+"</font>"
		}else{
			return "否";
		}
	}
	
	
	function withdrow(workitem_id){
		var panel = new Ext.FormPanel({
			 	frame:true,
			 	layout:'form',
			 	labelWidth:60,
			 	items:[{
	                   xtype:'textarea',
	                   fieldLabel:'收回原因',
	                   allowBlank:false,
		               msgTarget:'qtip',
		               vtype:'isNumberAlphaUnderlineChinese',
	                   name:'reason',
	                   id:'reason',
	                   width:330,
	                   maxLength:20            
	               }],
	             buttons: [{
		            text: '确认',
		            handler:function(){
		            if(panel.form.isValid()){
		                var reason = Ext.get("reason").dom.value;
		                window.close();  
		            	Ext.Ajax.request({
						     method : 'POST',                             
						     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/WithdrowWorkitem.action', 
						     success:function(request){            
						       var myResult = Ext.util.JSON.decode(request.responseText);
						       Ext.Msg.alert('信息',myResult.message,function(){document.location.reload();}); 		          
						     },
						     failure:function(){    
						       Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
						     },
						     params:{
						     	workitem_id:workitem_id,
						     	userId:userId,
						     	reason:reason
						     } 
				     	});
				     	}
		            }
	       		 },{
		            text: '重置',
		            handler:function(){
		            	panel.getForm().reset();
		            }
		        }]
	         });
       
		         var window = new Ext.Window({
		            title: '收回委托工作项',
					width: 440,
					height:140,
					layout: 'fit',
					plain:true,
					items:panel,
					buttonAlign:'center',
					modal:true
		           });
		           
				window.show();
	}
</script>
<div id='form-ct' width='100%'></div>
<div id='task-grid' width='100%'></div>
</head>
</html>